<?php

namespace app\platform\controller;

use think\facade\View;
use think\response\Json;

class Menu extends PlatformBase
{
    public function lists(): string
    {
        return View::fetch();
    }

    public function list(): Json
    {
        $menuBusiness = new \app\common\business\Menu();
        $menus        = $menuBusiness->getAllMenu();
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $menus['rows'], $menus['count']);
    }

    public function add(): string
    {
        $pid = $this->request->param('pid', 0, 'intval');
        View::assign(['pid' => $pid]);

        $menuBusiness = new \app\common\business\Menu();
        $parentMenu   = $menuBusiness->findMenuById($pid);
        View::assign(['parentMenu' => $parentMenu]);

        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $menuType  = $this->request->param('menu_type', 0, 'intval');
        $menuName  = $this->request->param('menu_name', '', 'trim');
        $menuCode  = $this->request->param('menu_code', '', 'trim');
        $path      = $this->request->param('path', '', 'trim');
        $iconLayUi = $this->request->param('icon_layui', '', 'trim');
        $status    = $this->request->param('status', 0, 'intval');
        $hide      = $this->request->param('hide', 0, 'intval');
        $pid       = $this->request->param('pid', 0, 'intval');
        $apiPath   = $this->request->param('api_path', '', 'trim');
        $sort      = $this->request->param('sort', 0, 'intval');

        $data = [
            'menu_type'  => $menuType,
            'menu_name'  => $menuName,
            'menu_code'  => $menuCode,
            'path'       => $path,
            'icon_layui' => $iconLayUi,
            'status'     => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'hide'       => $hide,
            'pid'        => $pid,
            'api_path'   => $apiPath,
            'sort'       => $sort,
        ];
        if (!$data['pid']) {
            $data['business_type'] = $this->request->param('business_type', 0, 'intval');
        }

        $validate = (new \app\platform\validate\Menu())->scene('add');
        if (!$validate->check($data)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), $validate->getError());
        }

        if (!isset($data['business_type']) && $data['pid'] == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $menuBusiness = new \app\common\business\Menu();
        $res          = $menuBusiness->addMenu($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $menuBusiness = new \app\common\business\Menu();
        $menu         = $menuBusiness->findMenuById($id);
        View::assign(["menu" => $menu]);

        if ($menu['pid'] != 0) {
            $parentMenu = $menuBusiness->findMenuById($menu['pid']);
            View::assign(['parentMenu' => $parentMenu]);
        }

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id        = $this->request->param('id', 0, 'intval');
        $menuType  = $this->request->param('menu_type', 0, 'intval');
        $menuName  = $this->request->param('menu_name', '', 'trim');
        $menuCode  = $this->request->param('menu_code', '', 'trim');
        $path      = $this->request->param('path', '', 'trim');
        $iconLayUi = $this->request->param('icon_layui', '', 'trim');
        $status    = $this->request->param('status', 0, 'intval');
        $hide      = $this->request->param('hide', 0, 'intval');
        $apiPath   = $this->request->param('api_path', '', 'trim');
        $sort      = $this->request->param('sort', 0, 'intval');

        $data = [
            'menu_type'  => $menuType,
            'menu_name'  => $menuName,
            'menu_code'  => $menuCode,
            'path'       => $path,
            'icon_layui' => $iconLayUi,
            'status'     => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'hide'       => $hide,
            'api_path'   => $apiPath,
            'sort'       => $sort,
        ];

        $validate = (new \app\platform\validate\Menu())->scene('edit');
        if (!$validate->check($data)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), $validate->getError());
        }

        $menuBusiness = new \app\common\business\Menu();
        $res          = $menuBusiness->updateMenu($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $menuBusiness = new \app\common\business\Menu();
        $res          = $menuBusiness->batchMenuSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $menuBusiness = new \app\common\business\Menu();
        $res          = $menuBusiness->deleteMenu($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}